<?php

class user_sms extends cms_controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('sms');
        $this->load->model('sms_message_model');
    }

    public function index() {
        $this->pass['total'] = get_total_sms();
        $this->pass['used'] = get_used_sms();
        $this->load->model('store_model', 'stores');
        $store = $this->stores->is_found(array('owner_id' => $this->user_id), array(), 'row', __CLASS__ . '/create/1', 'message_create_yourstore');
        $this->pass['result'] = $this->sms_message_model->get(array('store_id' => $store->id));
        $this->themes->render('user_sms/index', $this->pass);
    }

    public function send() {
        $this->load->model('store_model', 'stores');
        $store = $this->stores->is_found(array('owner_id' => $this->user_id), array(), 'row', __CLASS__ . '/create/1', 'message_create_yourstore');
        $subscribers = get_store_phones($store->id);
        if(!$subscribers){
            $this->themes->set_message(array('url' => __CLASS__, 'type' => 'error', 'message' => 'message_dont_have_subscribers'));
        }
        $total = unserialize($store->package_values);
        if (isset($total['sms'])) {
            if (get_used_sms() >= $total['sms']){
                $this->themes->set_message(array('url' => __CLASS__, 'type' => 'error', 'message' => 'message_sms_max'));
            }
            if ($this->input->post()) {
                $this->load->library('form_validation');
                $this->form_validation->set_rules('message', lang('label_sms_message'), 'trim|required');
                $this->form_validation->set_rules('smslanguage', lang('label_sms_message_language'), 'trim|required');
                if ($this->form_validation->run()) {
                    $language = $this->input->post('smslanguage');
                    $messgae = $this->input->post('message');
                    $length = strlen($messgae);
                    if (in_array($this->input->post('smslanguage'), array('ar', 'en'))) {
                        if (preg_match('/^[a-z0-9.-_]+$/', $msg)) {
                            $messgae = substr($messgae, 0, 160);
                        } else {
                            $messgae = substr($messgae, 0, 70);
                        }
                        $save['store_id'] = $store->id;
                        $save['message'] = $messgae;
                        if($id = $this->sms_message_model->save($save)){
                            $this->load->model('sms_phones_model');
                            foreach ($subscribers as $userid => $mobile){
                                $save = NULL;
                                $save['message_id'] = $id;
                                $save['user_id'] = $userid;
                                $save['phone'] = $mobile;
                                $this->sms_phones_model->save($save);
                            }
                            $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_sms_success_save'));
                        }else{
                            $this->themes->set_message(array('url' => current_url(), 'type' => 'error', 'message' => 'message_sms_error_save'));
                        }
                        
                    } else {
                        $this->themes->set_message(array('url' => __CLASS__, 'type' => 'error', 'message' => 'message_not_support_lang'));
                    }
                } else {
                    get_validation_errors();
                }
            }
            $this->themes->render('user_sms/send', $this->pass);
            return;
        }
        $this->themes->set_message(array('url' => __CLASS__, 'type' => 'error', 'message' => 'message_send_error'));
    }

    public function orders() {
        
    }

    public function add_order() {
        $data ['head_title'] = lang('label_sms_add_new_order');
        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());
            $this->form_validation->set_rules('amount', lang('label_wallet_amount'), 'required|trim|is_natural_no_zero');
            $this->form_validation->set_rules('bank_name', lang('label_wallet_bank'), 'required|trim');
            $this->form_validation->set_rules('to_bank', lang('label_wallet_to_bank'), 'required|trim');
            $this->form_validation->set_rules('sender_name', lang('label_wallet_sender_name'), 'required|trim');
            $this->form_validation->set_rules('account_number', lang('label_wallet_account_number'), 'required|trim');
            $this->form_validation->set_rules('note', lang('label_wallet_note'), 'trim');
            if ($this->form_validation->run()) {
                $amount = $this->input->post('amount');
                $sql_data = array(
                    'msg_num' => $this->get_num_msg($amount),
                    'msg_cost' => $amount,
                    'msg1_cost' => $this->sms->get_user_message_cost($this->user_id),
                    'user_id' => $this->user_id,
                    'create_date' => date('Y-m-d H:i:s'),
                );
                if ($this->db->insert('sms_orders', $sql_data)) {
                    $id = $this->db->insert_id();
                    $this->db->insert('wallet_bank', array(
                        'wallet_id' => $id,
                        'amount' => $amount,
                        'type' => 'sms_order',
                        'bank_name' => $this->input->post('bank_name'),
                        'to_bank' => $this->input->post('to_bank'),
                        'sender_name' => $this->input->post('sender_name'),
                        'account_number' => $this->input->post('account_number'),
                        'note' => $this->input->post('note')
                    ));
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array(
                        'url' => __CLASS__ . '/orders',
                        'type' => 'success',
                        'message' => 'message_sms_send_add_order_success'
                    ));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        } else {
            $this->themes->render(__CLASS__ . '/new_order', $data);
        }
    }

    public function get_num_msg_ajax() {
        $amount = $this->input->post('amount');

        $user_msg_cost = $this->sms->get_user_message_cost($this->user_id);

        $am = round($amount / $user_msg_cost);

        if ($am > $user_msg_cost) {
            echo lang('label_sms_get_num_msg_') . $am;
        } else {
            echo 0;
        }
    }

    public function get_num_msg($amount = 1) {
        $user_msg_cost = $this->sms->get_user_message_cost($this->user_id);

        $am = round($amount / $user_msg_cost);

        if ($am > $user_msg_cost) {
            return $am;
        } else {
            return 0;
        }
    }

    public function delete($id = false) {
        $this->sms_model->is_found(array(
            'id' => $id,
            'type' => 'fail'
                ), array(), FALSE, __CLASS__);
        $this->sms_model->delete(array(
            'id' => $id
        ));
        $this->themes->set_message(array(
            'url' => __CLASS__,
            'type' => 'success',
            'message' => 'message_delete_success'
        ));
    }

    public function resend($id = false) {
        $this->sms_model->is_found(array(
            'id' => $id,
            'type' => 'fail'
                ), array(), FALSE, __CLASS__);
        $this->sms_model->save(array(
            'type' => 'wait'
                ), array(
            'id' => $id
                ), true);
        $this->themes->set_message(array(
            'url' => __CLASS__,
            'type' => 'success',
            'message' => 'message_sms_send_resend_success'
        ));
    }

    public function get_cost() {
        $msg = $this->input->post('message');
        $msg_part = $this->sms->get_msg_part($msg);

        $mobile = $this->input->post('user_mobile');
        $mobile_part = $this->sms->get_mobile_length($mobile);

        echo lang('label_sms_msg_length') . strlen($msg) . ' ' . lang('label_sms_msg_part') . $msg_part . '  ' . lang('label_sms_mobile_part') . $mobile_part . ' ' . lang('label_sms_point') . ($mobile_part * $msg_part);
    }

    public function point_error() {
        $message ['url'] = current_url();
        $message ['type'] = 'error';
        $message ['message'] = 'message_sms_send_point_error';
        $this->themes->set_message($message);
    }

}

?>